package code.rocky.chapter04;

public class Suggest56 {
    public static void main(String[] args) {
        demo1();
        System.gc();
        demo2();
        System.gc();
        demo3();
        System.gc();
        demo4();
    }

    private static void demo1() {
        long startTime = System.nanoTime();
        StringBuilder sb = new StringBuilder("a");
        for (int i = 0; i < 50000; i++) {
            sb.append("c");
        }
        String str = sb.toString();
        long endTime = System.nanoTime();
        System.out.println("demo01 cost " + (endTime - startTime) + " ms");
    }

    private static void demo2() {
        long startTime = System.nanoTime();
        String sb = new String("a");
        for (int i = 0; i < 50000; i++) {
            sb += "c";
        }
        long endTime = System.nanoTime();
        System.out.println("demo02 cost " + (endTime - startTime) + " ms");
    }
    private static void demo3() {
        long startTime = System.nanoTime();
        String sb = new String("a");
        for (int i = 0; i < 50000; i++) {
            sb.concat("c");
        }
        long endTime = System.nanoTime();
        System.out.println("demo03 cost " + (endTime - startTime) + " ms");
    }
    private static void demo4() {
        long startTime = System.nanoTime();
        StringBuffer sb = new StringBuffer("a");
        for (int i = 0; i < 50000; i++) {
            sb.append("c");
        }
        long endTime = System.nanoTime();
        System.out.println("demo04 cost " + (endTime - startTime) + " ms");
    }

}
